package com.hualan.controller;

import com.hualan.pojo.PageResult;
import com.hualan.pojo.PricingStandard;
import com.hualan.pojo.PricingType;
import com.hualan.pojo.Result;
import com.hualan.service.ConsultantService;
import com.hualan.service.PricingStandardService;
import com.hualan.service.PricingTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value="/pricingStandard")
public class PricingStandardController {
    @Autowired
    private PricingStandardService pricingStandardService;
    @Autowired
    private PricingTypeService pricingTypeService;
    @Autowired
    private ConsultantService consultantService;

    @GetMapping(value="/search")
    public Result queryAllPricingStandard(String typeName,
                                          @RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10") Integer pageSize){
        PageResult pageResult = pricingStandardService.queryAllPricingStandard(typeName, page, pageSize);
        return Result.success(pageResult);

    }


    @PostMapping(value="/add")
    public Result addPricingStandard(@RequestBody PricingStandard pricingStandard){
        String typeName = pricingStandard.getTypeName();
        String name = pricingStandard.getName();

        if (typeName != null) {
            Integer typeId = pricingStandard.getTypeId();

            if (typeId == null) {
                // 尝试查找是否已有相同 typeName 的类型
                typeId = pricingTypeService.getTypeIdByTypeName(typeName);
                if (typeId == null) {
                    // 如果没找到，才新建类型
                    PricingType newType = new PricingType();
                    newType.setType(typeName);
                    pricingTypeService.addPricingType(newType);
                    typeId = newType.getId();
                }
                pricingStandard.setTypeId(typeId);
            } else {
                // 如果 typeId 存在，并且 typeName 有值，可以选择更新类型名
                PricingType updateType = new PricingType();
                updateType.setId(typeId);
                updateType.setType(typeName);
                pricingTypeService.updatePricingType(updateType);
            }
        }

        // 新增逻辑：处理 consultantName -> consultantId
        if (name != null && !name.isEmpty()) {
            Integer consultantId = pricingStandardService.getConsultantIdByName(name);
            if (consultantId == null) {
                return Result.error("顾问不存在，请确认名称");
            } else {
                pricingStandard.setConsultantId(consultantId);
            }
        }

        Result result = pricingStandardService.addPricingStandard(pricingStandard);
        return result;
    }




    @PutMapping(value="/update")
    public Result updatePricingStandard(@RequestBody PricingStandard pricingStandard){
        System.out.println("接收到的参数：" + pricingStandard);

        String typeName = pricingStandard.getTypeName();
        String name = pricingStandard.getName();

        // 处理 typeName -> typeId（仿照 add 逻辑）
        if (typeName != null) {
            Integer typeId = pricingStandard.getTypeId();

            if (typeId != null) {
                // 获取当前 typeId 对应的 typeName
                String oldTypeName = pricingTypeService.getTypeNameById(typeId);

                // 如果新旧 typeName 不同，则新建一个类型
                if (oldTypeName == null || !oldTypeName.equals(typeName)) {
                    Integer existingTypeId = pricingTypeService.getTypeIdByTypeName(typeName);
                    if (existingTypeId != null) {
                        pricingStandard.setTypeId(existingTypeId);
                    } else {
                        PricingType newType = new PricingType();
                        newType.setType(typeName);
                        pricingTypeService.addPricingType(newType);
                        pricingStandard.setTypeId(newType.getId());
                    }
                }
            } else {
                // 如果 typeId 不存在，说明是新增类型（理论上不会出现在 update 中）
                Integer existingTypeId = pricingTypeService.getTypeIdByTypeName(typeName);
                if (existingTypeId != null) {
                    pricingStandard.setTypeId(existingTypeId);
                } else {
                    PricingType newType = new PricingType();
                    newType.setType(typeName);
                    pricingTypeService.addPricingType(newType);
                    pricingStandard.setTypeId(newType.getId());
                }
            }
        }

        // 处理 name -> consultantId（仿照 add 逻辑）
        if (name != null && !name.isEmpty()) {
            Integer consultantId = pricingStandardService.getConsultantIdByName(name);
            if (consultantId == null) {
                return Result.error("顾问不存在，请确认名称");
            } else {
                pricingStandard.setConsultantId(consultantId);
            }
        }

        // 执行最终的更新操作
        Result result = pricingStandardService.updatePricingStandard(pricingStandard);
        return result;
    }




    @PutMapping(value="/delete")
    public Result deletePricingStandard(@RequestBody java.util.List<java.lang.Integer> ids){
        Result result = pricingStandardService.deletePricingStandard(ids);
        return result;
    }
}
